package com.example.controller.user;

import com.example.dto.ShoppingCartDTO;
import com.example.entity.ShoppingCar;
import com.example.result.Result;
import com.example.service.ShoppingCarService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@Tag(name = "购物车")
@RequestMapping("/user/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCarService shoppingCarService;

    /**
     *添加购物车
     * @return
     */
    @PostMapping("/add")
    @Operation(summary = "添加购物车")
    public Result shoppingCar(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("添加购物车,{}",shoppingCartDTO);
        shoppingCarService.shoppingCar(shoppingCartDTO);
        return Result.success();

    }

    /**
     * 查看购物车
     */
    @GetMapping("/list")
    @Operation(summary = "查看购物车")
    public Result getShoppingCar(){
        log.info("查看购物车：");
        List<ShoppingCar> shoppingCarList=shoppingCarService.getShoppingCar();
        return Result.success(shoppingCarList);
    }

    /**
     * 清空购物车
     */
    @DeleteMapping("/clean")
    @Operation(summary = "清空购物车")
    public Result cleanShoppingCar(){
        log.info("清空购物车");
        shoppingCarService.cleanShoppingCar();
        return Result.success();
    }

    /**
     * 删除购物车中一个商品
     * @param shoppingCartDTO
     * @return
     */
    @PostMapping("/sub")
    @Operation(summary = "删除购物车中一个商品")
    public Result sub(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("删除购物车中一个商品，商品：{}", shoppingCartDTO);
        shoppingCarService.subShoppingCart(shoppingCartDTO);
        return Result.success();
    }

}
